<?php
namespace app\common\exception;

use think\config;
use Exception;
use think\exception\Handle;
use think\exception\HttpException;

class Http extends Handle
{
    public function render(Exception $e)
    {
        $debug = 'false';
        if ($debug == 'true') {
            // return parent::render($e);
            Header("HTTP/1.1 303 See Other"); 
            header('location:'.$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].'/errpage/404.html');
        } else {
            // dump($e);die;
            // return parent::render($e);
            //参数异常
            $code = 400;
            if ($e instanceof ValidateException) {
                $code = 422;
            }
            // 请求异常
            if ($e instanceof HttpException) {
                $code = $e->getStatusCode();
            }
            $message = $e->getMessage();
            $message = str_replace('module not exists','模块不存在',$message);
            $message = str_replace('method not exists','方法不存在',$message);
            $message = str_replace('template not exists','模板不存在',$message);
            $message = str_replace('Parse error: syntax error','语法错误',$message);
            
            $html = "<meta charset='utf-8'><title>error</title>";
            $html .= "<link rel='stylesheet' media='screen' href='/errpage/style.css'>";
            $html .= "<body>";
            $html .= "   <p class='error' style='font: 700 18em helvetica neue;text-shadow: #014767 0 2px 17px;'>".$code."</p>";
            $html .= "   <div class='content' style='width: 770px;'>";
            $html .= "       <p style='margin-top:10px;'>错误提示：</p>";
            $html .= "       <h4 style='padding-left:20px;word-wrap: break-word;'>".$message."</h4>";
            $html .= "       <p style='margin-top:10px;'>错误文件：</p>";
            $html .= "       <h4 style='padding-left:20px;word-wrap: break-word;'>".$e->getFile()."</h4>";
            $html .= "       <p style='margin-top:10px;'>错误位置：</p>";
            $html .= "       <h4 style='padding-left:20px;word-wrap: break-word;'>第".$e->getLine()."行</h4>";
            $html .= "   </div>";
            $html .= "</body>";
            echo $html;
            die;
        }
    }
}